package cn.edu.scau.cmi.huangmaoqin.hibernate.clientController;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cn.edu.scau.cmi.huangmaoqin.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.huangmaoqin.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.huangmaoqin.hibernate.domain.Student;
import cn.edu.scau.cmi.huangmaoqin.hibernate.domain.Teacher;
import javafx.event.ActionEvent;

public class StudentManageController {
	@FXML
	private Label flabel;
	@FXML
	private Label slabel;
	@FXML
	private Label tlabel;
	@FXML
	private TextField first;
	@FXML
	private TextField second;
	@FXML
	private TextField third;
	@FXML
	private Button start;
	@FXML
	private Button addS;
	@FXML
	private Button addT;
	@FXML
	private Button updateS;
	@FXML
	private Button updateT;
	@FXML
	private Button deleteS;
	@FXML
	private Button deleteT;
	@FXML
	private Button choseIT;
	@FXML
	private Button updateIT;
	@FXML
	private Button choseIS;
	@FXML
	private Button updateIS;
	@FXML
	private TextArea show;
	@FXML
	private Button listAllS;
	@FXML
	private Button listAllT;

	private String operation = null;
	private boolean isStudent = true;

	// Event Listener on Button.onAction
	@FXML
	public void btnEvent(ActionEvent event) {
		// 得到按钮实例
		Button btn = (Button) event.getSource();
		if (btn != start && btn != listAllS && btn != listAllT) {
			first.clear();
			second.clear();
			third.clear();
		}
		if (btn == start) {
			first.setEditable(false);
			second.setEditable(false);
			third.setEditable(false);
			if (first.getText().length() <= 0)
				return;
			if (isStudent) {
				studentOperation(operation, null);
				listStudent();
			} else {
				teacherOperation(operation, null);
				listTeacher();
			}
			isStudent = true;
			operation = null;
		} else if (btn == addS) {
			first.setEditable(true);
			second.setEditable(true);
			third.setEditable(true);
			flabel.setText("学生ID：");
			slabel.setText("学生姓名：");
			tlabel.setText("老师ID：");
			operation = btn.getText();
			isStudent = true;
		} else if (btn == addT) {
			first.setEditable(true);
			second.setEditable(true);
			third.setEditable(true);
			flabel.setText("老师ID：");
			slabel.setText("老师姓名：");
			tlabel.setText("学生ID：");
			isStudent = false;
			operation = btn.getText();
		} else if (btn == updateS) {
			first.setEditable(true);
			second.setEditable(true);
			third.setEditable(true);
			flabel.setText("要修改的学生ID：");
			slabel.setText("修改后的性名：");
			tlabel.setText("修改后的老师ID：");
			isStudent = true;
			operation = btn.getText();
		} else if (btn == updateT) {
			first.setEditable(true);
			second.setEditable(true);
			third.setEditable(true);
			flabel.setText("要修改的老师ID：");
			slabel.setText("修改后的性名：");
			tlabel.setText("修改后的学生ID：");
			operation = btn.getText();
			isStudent = false;
		} else if (btn == deleteS) {
			first.setEditable(true);
			flabel.setText("要删除的学生ID：");
			slabel.setText("");
			tlabel.setText("");
			isStudent = true;
			operation = btn.getText();
		} else if (btn == deleteT) {
			first.setEditable(true);
			flabel.setText("要删除的老师ID：");
			slabel.setText("");
			tlabel.setText("");
			isStudent = false;
			operation = btn.getText();
		} else if (btn == choseIS) {
			first.setEditable(true);
			second.setEditable(true);
			flabel.setText("进行选择的老师ID：");
			slabel.setText("要选择指导的学生ID：");
			tlabel.setText("");
			isStudent = false;
			operation = btn.getText();

		} else if (btn == choseIT) {
			first.setEditable(true);
			second.setEditable(true);
			flabel.setText("进行选择的学生ID：");
			slabel.setText("要选择的老师ID：");
			tlabel.setText("");
			isStudent = true;
			operation = btn.getText();
		} else if (btn == updateIS) {
			first.setEditable(true);
			second.setEditable(true);
			flabel.setText("进行修改的老师ID：");
			slabel.setText("修改后指导的学生ID：");
			tlabel.setText("");
			isStudent = false;
			operation = btn.getText();
		} else if (btn == updateIT) {
			first.setEditable(true);
			second.setEditable(true);
			flabel.setText("进行修改的学生ID：");
			slabel.setText("修改后的指导老师ID：");
			tlabel.setText("");
			operation = btn.getText();
			isStudent = true;
		} else if (btn == listAllS) {
			listStudent();
		} else if (btn == listAllT) {
			listTeacher();
		}
	}

	public Student studentOperation(String operation, Long sid) {
		StudentDAO studentDAO = new StudentDAO();
		// 得到学生学号
		Long id = Long.parseLong(first.getText());
		// 得到学生姓名
		String name = second.getText();
		// 得到老师工号
		Long tid = (third.getText().length() > 0 ? Long.parseLong(third.getText()) : null);
		// 得到学生实例
		Student student = studentDAO.findById(id);
		// 得到老师实例
		Teacher teacher = teacherOperation("查找老师", tid);
		// 得到学生原来老师实例
		Teacher oldT = (student == null ? null : student.getTeacher());
		switch (operation) {
		case "查找学生":
			student = (sid == null ? null : studentDAO.findById(sid));
			break;
		case "添加学生":
			student = new Student(id, name, teacher);
			studentDAO.save(student);
			break;
		case "修改学生":
			student.setName(name);
			student.setTeacher(teacher);
			studentDAO.update(student, oldT);
			break;
		case "删除学生":
			studentDAO.delete(student);
			break;
		case "选择指导老师":
		case "修改指导老师":
			// 得到选择的指导老师id
			tid = (second.getText().length() > 0 ? Long.parseLong(second.getText()) : null);
			teacher = teacherOperation("查找老师", tid);
			student.setTeacher(teacher);
			studentDAO.update(student, oldT);
			break;
		default:
			break;
		}
		return student;
	}

	public Teacher teacherOperation(String operation, Long tid) {
		TeacherDAO teacherDAO = new TeacherDAO();
		StudentDAO studentDAO = new StudentDAO();
		// 老师实例
		Teacher teacher = null;
		// 得到老师工号
		Long id = Long.parseLong(first.getText());
		// 得到老师姓名
		String name = second.getText();
		// 得到待指导学生学号串
		String[] sidString = null;

		if (operation.equals("选择待指导学生") || operation.equals("修改待指导学生"))
			sidString = (second.getText().length() > 0 ? second.getText().split(",|，") : new String[0]);
		else
			sidString = (third.getText().length() > 0 ? third.getText().split(",|，") : new String[0]);

		// 待指导学生
		Set<Student> students = new HashSet<Student>();
		for (int i = 0; i < sidString.length; i++) {
			Long tempID = Long.parseLong(sidString[i]);
			Student student = studentDAO.findById(tempID);
			students.add(student);
		}
		// 得到老师实例
		teacher = teacherDAO.findById(id);
		Set<Student> oldStudents = (teacher == null ? null : teacher.getStudents());
		switch (operation) {
		case "查找老师":
			teacher = (tid == null ? null : teacherDAO.findById(tid));
			break;
		case "添加教师":
			teacher = new Teacher(id, name);
			// 先新建老师才能创建与学生的关系
			teacherDAO.save(teacher, students);
			break;
		case "修改教师":
			teacher.setName(name);
			teacher.setStudents(students);
			teacherDAO.update(teacher, oldStudents);
			break;
		case "删除教师":
			teacherDAO.delete(teacher);
			break;
		case "选择待指导学生":
		case "修改待指导学生":
			teacher.setStudents(students);
			teacherDAO.update(teacher, oldStudents);
			break;
		default:
			break;
		}
		return teacher;
	}

	public void listStudent() {
		show.clear();
		StudentDAO studentDAO = new StudentDAO();
		List students = studentDAO.findAll();
		for (Object object : students) {
			Student student = (Student) object;
			if (student.getTeacher() != null)
				show.appendText("ID为" + student.getId() + "，名字为" + student.getName() + "的学生，其老师ID为："
						+ student.getTeacher().getId() + "，名字为：" + student.getTeacher().getName());
			else
				show.appendText("ID为" + student.getId() + "，名字为" + student.getName() + "的学生，无指导老师");
			show.appendText("\n");
		}
	}

	public void listTeacher() {
		show.clear();
		TeacherDAO teacherDAO = new TeacherDAO();
		List teachers = teacherDAO.findAll();
		for (Object object : teachers) {
			Teacher teacher = (Teacher) object;
			show.appendText("ID为" + teacher.getId() + "，名字为" + teacher.getName() + "的老师，");
			if (teacher.getStudents().size() > 0) {
				show.appendText("指导的学生有：");
				for (Object student : teacher.getStudents()) {
					show.appendText(((Student) student).getId() + "-" + ((Student) student).getName() + " ; ");
				}
			} else {
				show.appendText("没有指导学生;");
			}
			show.appendText("\n");
		}
	}
}
